﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebApplication3.Model;
namespace WebApplication3.Data
{
    [Route("api/[controller]")]
    [ApiController]
    public class ResultController : ControllerBase
    {
        private readonly ApplicationDbContext _context;

        public ResultController(ApplicationDbContext context)
        {
            _context = context;
        }

        [HttpPost("getTestResult")]
        public IActionResult GetTestResult([FromBody] ScoreAndCategoryRequest request)
        {
            if (request == null || request.Score < 0 || request.Category < 0)
            {
                return BadRequest("Invalid request.");
            }

            var testResult = _context.TestResults
                .FirstOrDefault(r => r.MinScore <= request.Score && r.MaxScore >= request.Score && r.CategoryId == request.Category);

            if (testResult == null)
            {
                return NotFound("Test result not found.");
            }

            return Ok(new { result = testResult.Result });
        }
        [HttpPost("saveTestScore")]
        public async Task<IActionResult> SaveTestScore([FromBody] ScoreAndCategoryRequest request)
        {
            if (request == null || request.Score < 0 || request.Category < 0)
            {
                return BadRequest("Invalid request.");
            }

            var testScore = new TestScore
            {
                Score = request.Score,
                Category = request.Category,
                TestDate = DateTime.UtcNow
            };

            _context.TestScores.Add(testScore);
            await _context.SaveChangesAsync();

            return Ok(new { message = "Test score saved successfully." });
        }

        [HttpGet("getAllTestScores")]
        public IActionResult GetAllTestScores()
        {
            var testScores = _context.TestScores.ToList();
            if (testScores == null || !testScores.Any())
            {
                return NotFound("No test scores found.");
            }

            return Ok(testScores);
        }
        [HttpGet("getTestScoresWithCategoryNames")]
        public IActionResult GetTestScoresWithCategoryNames()
        {
            var testScores = _context.TestScores
                .Join(_context.Categories,
                      score => score.Category,
                      category => category.Id,
                      (score, category) => new TestScoreDTO
                      {
                          Id = score.Id,
                          Score = score.Score,
                          CategoryName = category.Name,
                          TestDate = score.TestDate
                      })
                .ToList();

            if (testScores == null || !testScores.Any())
            {
                return NotFound("未找到测试分数。");
            }

            return Ok(testScores);
        }
        public class TestScoreDTO
        {
            public int Id { get; set; }
            public int Score { get; set; }
            public string CategoryName { get; set; }
            public DateTime TestDate { get; set; }
        }

        public class ScoreAndCategoryRequest
        {
            public int Score { get; set; }
            public int Category { get; set; }
        }
    }
}

